# coding: utf-8

from .handlers import account
from django.conf.urls import url
from django.views.decorators.csrf import csrf_exempt

import os


def CheckUserIsParentForApiCaller(request, *args, **kwargs):
    realCall = kwargs.pop('_viewFunc')
    return realCall(request, *args, **kwargs)

urlpatterns_api = []

def addPreCheck(urlpatterns, checkFunC):
    checkFunC = csrf_exempt(checkFunC)
    for pattern in urlpatterns:
        urlRegx = pattern[0]
        if type(urlRegx) != list:
            urlRegList = [urlRegx]
        else:
            urlRegList = urlRegx

        viewFunc = pattern[1]
        if len(pattern) == 2:
            for one in urlRegList:
                urlpatterns_api.append(url(one, checkFunC, {'_viewFunc': viewFunc}))

        elif len(pattern) == 3:
            otherPara = pattern[2]
            otherPara['_viewFunc'] = viewFunc
            for one in urlRegList:
                urlpatterns_api.append(url(one, checkFunC, otherPara))
        else:
            print('!!unsupported url format : %s' % pattern)
            os._exit(3)

urlpatterns = [
    # (r'^$', account.handler.dispatch),
    ([r'^auth/$', r'^auth/(?P<auth_type>\w+)/$'], account.handler.dispatch),
]
addPreCheck(urlpatterns, CheckUserIsParentForApiCaller)
urlpatterns = urlpatterns_api
